package cn.iocoder.yudao.module.member.service.usersharelogs;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.member.controller.admin.usersharelogs.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.usersharelogs.UserShareLogsDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.usersharelogs.UserShareLogsMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 用户推广日志 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserShareLogsServiceImpl implements UserShareLogsService {

    @Resource
    private UserShareLogsMapper userShareLogsMapper;

    @Override
    public Long createUserShareLogs(UserShareLogsSaveReqVO createReqVO) {
        // 插入
        UserShareLogsDO userShareLogs = BeanUtils.toBean(createReqVO, UserShareLogsDO.class);
        userShareLogsMapper.insert(userShareLogs);
        // 返回
        return userShareLogs.getId();
    }

    @Override
    public void updateUserShareLogs(UserShareLogsSaveReqVO updateReqVO) {
        // 校验存在
        validateUserShareLogsExists(updateReqVO.getId());
        // 更新
        UserShareLogsDO updateObj = BeanUtils.toBean(updateReqVO, UserShareLogsDO.class);
        userShareLogsMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserShareLogs(Long id) {
        // 校验存在
        validateUserShareLogsExists(id);
        // 删除
        userShareLogsMapper.deleteById(id);
    }

    private void validateUserShareLogsExists(Long id) {
        if (userShareLogsMapper.selectById(id) == null) {
            throw exception(USER_SHARE_LOGS_NOT_EXISTS);
        }
    }

    @Override
    public UserShareLogsDO getUserShareLogs(Long id) {
        return userShareLogsMapper.selectById(id);
    }

    @Override
    public PageResult<UserShareLogsDO> getUserShareLogsPage(UserShareLogsPageReqVO pageReqVO) {
        return userShareLogsMapper.selectPage(pageReqVO);
    }

}